package com.dealglobe.feeder.bvd;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.bvdep.webservices.ArrayOfSelectionParameter;
import com.bvdep.webservices.RemoteAccess;
import com.bvdep.webservices.RemoteAccessSoap;
import com.bvdep.webservices.ResultFormatting;
import com.bvdep.webservices.SelectionParameter;
import com.bvdep.webservices.SelectionResult;

public class BvdFeeder {

    private static String QUERY = 
            "DEFINE P1 AS [Parameters.DimensionSelName=NrWebSiteAddresses;NrWebSiteAddresses.Index=0;]," +
    		"P2 AS [Parameters.Currency=SESSION;Parameters.Units=3;Parameters.DimensionSelName=NrOfYears;NrOfYears.Index=0;]," +
    		"P3 AS [Parameters.DimensionSelName=NrPrimNaicsCode2007;NrPrimNaicsCode2007.Index=0;]," +
            "P4 AS [Parameters.DimensionSelName=NrOfYears;NrOfYears.Index=0;]; " +
    		"SELECT LINE GENERAL_INFO.NAME AS NAME, LINE CONTACT_INFO.CITY AS CITY, LINE CONTACT_INFO.COUNTRY AS COUNTRY, " +
    		"LINE CONTACT_INFO.STATE_US AS STATE_US, LINE CONTACT_INFO.WEBSITE " +
    		"USING P1 AS WEBSITE, LINE IDENT_CODE.BVDID AS BVDID, LINE IDENT_CODE.DB AS DB, " +
    		"LINE SIZE_GROUP_INFO.NACE2_MAIN_SECTION AS NACE2_MAIN_SECTION, LINE SIZE_GROUP_INFO.MAJOR_SECTOR AS MAJOR_SECTOR, " +
    		"LINE SIZE_GROUP_INFO.CATEGORY_OF_COMPANY AS CATEGORY_OF_COMPANY, " +
    		"LINE SIZE_GROUP_INFO.OPRE USING P2 AS OPRE, LINE SIZE_GROUP_INFO.PL USING P2 AS PL, " +
    		"LINE SIZE_GROUP_INFO.SIZE_EMPL AS SIZE_EMPL, LINE ACTIVITIES.TRADDESCEN AS TRADDESCEN, " +
    		"LINE ACTIVITIES.TRADDESCOR AS TRADDESCOR, LINE PEERGROUP.OPGNAME AS OPGNAME, " +
    		"LINE PEERGROUP.OPGDESC AS OPGDESC, LINE PEERGROUP.OPGSIZE AS OPGSIZE, LINE ACTIVITIES.NACECCOD2 AS NACECCOD2, " +
    		"LINE ACTIVITIES.NAICSPCOD2007 USING P3 AS NAICSPCOD2007 FROM RemoteAccess.A";

    private static String QUERY_EBIDTA = 
            "DEFINE P1 AS [Parameters.DimensionSelName=NrWebSiteAddresses;NrWebSiteAddresses.Index=0;]," +
            "P2 AS [Parameters.Currency=SESSION;Parameters.Units=3;Parameters.DimensionSelName=NrOfYears;NrOfYears.Index=0;]," +
            "P3 AS [Parameters.DimensionSelName=NrPrimNaicsCode2007;NrPrimNaicsCode2007.Index=0;]," +
            "P4 AS [Parameters.DimensionSelName=NrOfYears;NrOfYears.Index=0;]; " +
            "SELECT LINE DETAILLED_PL.313018 USING P2 AS EBITDA FROM RemoteAccess.C";
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        RemoteAccessSoap ws = null;
        String sessionHandle =  null;
        try {
            RemoteAccess remoteAccess = new RemoteAccess();
            ws = remoteAccess.getRemoteAccessSoap();
            sessionHandle = ws.open("DealGlobeWS", "Il48GzeZ4");
            
//            String availableModels = ws.getAvailableModels(sessionHandle);
//            
//            DocumentBuilderFactory db = DocumentBuilderFactory.newInstance();
//            Document doc = db.newDocumentBuilder().parse(new ByteArrayInputStream(availableModels.getBytes()));
//            NodeList nl = doc.getElementsByTagName("Model");
//            for(int i=0; i < nl.getLength(); i++) {
//                Element e = (Element) nl.item(i);
//                String modelId = e.getAttribute("id");
//                String allAvailableFields = ws.getAllAvailableFields(sessionHandle, modelId);
//                System.out.println("modelId: " + modelId + " available fields: " + allAvailableFields);
//            } 
            
            ArrayOfSelectionParameter array = new ArrayOfSelectionParameter();
            SelectionParameter sp = new  SelectionParameter();
            sp.setParameterId("Id");
            sp.setParameterValue("9XEGB");
            array.getSelectionParameter().add(sp);
            SelectionResult selectionResult = ws.find(sessionHandle, "DNOrbisNeo:bvd.OrbisNeo.Search.Companies.SearchStepDictionary+WorldRegions", array );
//            System.out.println("zzzzzzz " + selectionResult.getSelectionCount());
//            SelectionResult selectionResult = ws.findByName(sessionHandle, "BVD");
//            
            String data = ws.getData(sessionHandle, selectionResult, QUERY, 0, 3, ResultFormatting.XML);
            System.out.println("result count: " + selectionResult.getSelectionCount() + ", token: " + selectionResult.getToken() + " data: " + data);
            
            data = ws.getData(sessionHandle, selectionResult, QUERY_EBIDTA, 0, 3, ResultFormatting.XML);
            System.out.println("result count: " + selectionResult.getSelectionCount() + ", token: " + selectionResult.getToken() + " data: " + data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sessionHandle != null) {
                ws.close(sessionHandle);
            }
        }
        
        
      }

}
